import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: () => import('@/views/dashboard/index'),
        meta: { title: '工作台', icon: 'dashboard' }
      }
    ]
  },
  {
    path: '/manage-base-info',
    component: Layout,
    meta: { title: '基础信息管理', icon: 'dashboard' },
    children: [
      {
        path: 'warehouse',
        name: 'warehouse',
        component: () => import('@/views/manage/warehouse/index'),
        meta: { title: '仓库管理', icon: 'dashboard' },
        children: []
      },
      {
        // 新建仓库
        path: 'warehouse/details/:id',
        name: 'details',
        component: () => import('@/views/manage/warehouse/details'),
        hidden: true
      },
      {
        path: 'area',
        name: 'area',
        component: () => import('@/views/manage/area/index'),
        meta: { title: '库区管理', icon: 'dashboard' },
        children: [
          {
            path: 'area/details/:id',
            name: 'details',
            component: () => import('@/views/manage/area/details'),
            hidden: true
          }
        ]
      },
      {
        path: 'location',
        name: 'location',
        component: () => import('@/views/manage/location/index'),
        meta: { title: '库位管理', icon: 'dashboard' }
      },
      {
        path: 'location-view',
        name: 'location-view',
        component: () => import('@/views/manage/location-view/index'),
        meta: { title: '库位视图', icon: 'dashboard' }
      },
      {
        path: 'goods',
        name: 'goods',
        component: () => import('@/views/manage/goods/index'),
        meta: { title: '货品管理', icon: 'dashboard' }
      },
      {
        path: 'goods-type',
        name: 'goods-type',
        component: () => import('@/views/manage/goods-type/index'),
        meta: { title: '货品类型管理', icon: 'dashboard' }
      }
    ]
  },

  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
